package com.resource.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.resource.utils.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import com.resource.constant.OfficeApiConstants;
import com.resource.converter.CustomerLinkmanConverter;
import com.resource.dto.CustomerLinkmanSaveDto;
import com.resource.query.CustomerLinkmanQuery;
import com.resource.request.CustomerLinkmanSaveRequest;
import com.resource.response.CustomerLinkmanResponse;
import com.resource.response.OfficeResponse;
import com.resource.service.CustomerLinkmanService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 客户联系人管理
 * 
 * @Author: xiongbo.wan
 * @Date: 19:21 2020/9/22 0022
 */
@Slf4j
@Service
public class CustomerLinkmanServiceImpl implements CustomerLinkmanService {

	@Resource
	private RestTemplate restTemplate;
	@Resource
	private CustomerLinkmanConverter customerLinkmanConverter;
	@Value("${office.api.url}")
	private String apiUrl;
	@Value("${office.app.user}")
	private String user;

	@Override
	public void create(CustomerLinkmanSaveDto dto) {
		CustomerLinkmanSaveRequest customerLinkmanSaveRequest = customerLinkmanConverter.dtoToRequest(dto);
		String uri = String.format(OfficeApiConstants.SAVE_CUSTOMER_LINKMAN_URL, apiUrl);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.POST,
				JSONUtil.toJsonStr(customerLinkmanSaveRequest),user);
		log.info("【customerLinkman create request】{}", JSONUtil.toJsonStr(entity));
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.POST, entity,
				OfficeResponse.class);
		log.info("【customerLinkman create response!】{}", JSONUtil.toJsonStr(exchange));
		ValidateUtils.checkResponse(exchange);
	}

	@Override
	public void remove(Long id) {
		String uri = String.format(OfficeApiConstants.CUSTOMER_LINKMAN_URL, apiUrl, id);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.DELETE, OfficeApiConstants.IS_NULL,user);
		log.info("【customerLinkman remove request】{}", JSONUtil.toJsonStr(entity));
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.DELETE, entity,
				OfficeResponse.class);
		log.info("【customerLinkman remove response!】{}", JSONUtil.toJsonStr(exchange));
		ValidateUtils.checkResponse(exchange);
	}

	@Override
	public String modify(CustomerLinkmanSaveDto dto, String id) {
		CustomerLinkmanSaveRequest customerLinkmanSaveRequest = customerLinkmanConverter.dtoToRequest(dto);
		String uri = String.format(OfficeApiConstants.CUSTOMER_LINKMAN_URL, apiUrl, id);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.PUT,
				JSONUtil.toJsonStr(customerLinkmanSaveRequest),user);
		log.info("【customerLinkman modify request】{}", JSONUtil.toJsonStr(entity));
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.PUT, entity,
				OfficeResponse.class);
		log.info("【customerLinkman modify response!】{}", JSONUtil.toJsonStr(exchange));
		ValidateUtils.checkResponse(exchange);
		return "修改成功！";
	}

	@Override
	public CustomerLinkmanResponse load(Long id) {
		String uri = String.format(OfficeApiConstants.CUSTOMER_LINKMAN_URL, apiUrl, id);
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.GET, OfficeApiConstants.IS_NULL,user);
		log.info("【customerLinkman detail request】{}", JSONUtil.toJsonStr(entity));
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.GET, entity,
				OfficeResponse.class);
		log.info("【customerLinkman detail response!】{}", JSONUtil.toJsonStr(exchange));
		if (!HttpStatus.OK.equals(exchange.getStatusCode())) {
			throw new ServiceException("请求有误！");
		}
		OfficeResponse officeResponse = ValidateUtils.checkResponse(exchange);
		return BeanUtil.toBean(officeResponse.getData(), CustomerLinkmanResponse.class);
	}

	@Override
	public List<CustomerLinkmanResponse> list(CustomerLinkmanQuery customerLinkmanQuery) {
		Map<String, List<String>> condition;
		try {
			condition = SearchUtils.getQuery(customerLinkmanQuery);
		} catch (Exception e) {
			throw new ServiceException("获取查询条件失败");
		}
		String uri = String.format(OfficeApiConstants.LINKMAN_SEARCH, apiUrl, customerLinkmanQuery.getPage(),
				customerLinkmanQuery.getLimit());
		Map<String, Object> params = new HashMap<>(4);
		params.put("search", JSONUtil.toJsonStr(condition));
		HttpEntity<String> entity = HttpUtils.setHttpEntity(HttpMethod.GET, JSONUtil.toJsonStr(customerLinkmanQuery),user);
		log.info("【customerLinkman list request】{}【url】{}", JSONUtil.toJsonStr(entity), uri);
		ResponseEntity<OfficeResponse> exchange = restTemplate.exchange(uri, HttpMethod.GET, entity,
				OfficeResponse.class, params);
		log.info("【customerLinkman list response!】{}", JSONUtil.toJsonStr(exchange));
		OfficeResponse officeResponse = ValidateUtils.checkResponse(exchange);
		PageInfo<CustomerLinkmanResponse> page = Convert
				.convert(new TypeReference<PageInfo<CustomerLinkmanResponse>>() {
				}, officeResponse.getData());
		return page.getList();
	}

	@Override
	public void modifyByContract(CustomerLinkmanSaveDto dto) {
		if (CheckUtil.isEmpty(dto.getLinkmanId())) {
			throw new ServiceException("联系人id为空！");
		}
		CustomerLinkmanResponse detail = load(Long.valueOf(dto.getLinkmanId()));
		if (BeanUtil.isEmpty(detail)) {
			throw new ServiceException("详情数据不存在！");
		}
		CustomerLinkmanSaveDto saveDto = BeanUtil.copyProperties(detail, CustomerLinkmanSaveDto.class);
		saveDto.setEmail(dto.getEmail());
		saveDto.setMobilePhoneNumber(dto.getMobilePhoneNumber());
		modify(saveDto, saveDto.getLinkmanId());
	}
}
